<?php


namespace App\Admin\Controller\Wechat;


use App\Admin\Controller\AdminBaseController;
use App\Admin\Requests\Wechat\MiniProgramTemplateCreateRequest;
use App\Admin\Requests\Wechat\MiniProgramTemplateUpdateAuditItemListRequest;
use App\Admin\Requests\Wechat\MiniProgramTemplateUpdateDomainRequest;
use App\Admin\Requests\Wechat\MiniProgramTemplateUpdateRequest;
use App\Admin\Services\Wechat\MiniProgramTemplateService;

class MiniProgramTemplateController extends AdminBaseController {
    public $exceptActionList = ['getTemplateListForSearchFilter', 'getTemplateListByAppProjectId'];
    private $_service;

    public function __construct(MiniProgramTemplateService $service) {
        $this->_service = $service;
    }

    /**
     * 代码模板列表
     *
     * @return mixed
     */
    public function page() {
        return $this->success($this->_service->page());
    }

    /**
     * 新建代码模板
     *
     * @param MiniProgramTemplateCreateRequest $request
     * @return mixed
     * @throws \App\Admin\Exceptions\Wechat\MiniProgramTemplateException
     */
    public function store(MiniProgramTemplateCreateRequest $request) {
        return $this->success($this->_service->store($request));
    }

    /**
     * 删除指定id的代码模板
     *
     * @param $id
     * @return mixed
     * @throws \App\Admin\Exceptions\Wechat\MiniProgramTemplateException
     */
    public function delete($id) {
        return $this->success($this->_service->delete($id));
    }

    /**
     * 更新指定id的代码模板
     *
     * @param $id
     * @param MiniProgramTemplateUpdateRequest $request
     * @return mixed
     * @throws \App\Admin\Exceptions\Wechat\MiniProgramTemplateException
     */
    public function update($id, MiniProgramTemplateUpdateRequest $request) {
        return $this->success($this->_service->update($id, $request));
    }

    /**
     * 根据项目类型id获取模板列表
     *
     * @param $appProjectTypeId
     * @return mixed
     * @throws \App\Admin\Exceptions\App\ProjectType\ProjectTypeException
     */
    public function getTemplateListByAppProjectId($appProjectTypeId) {
        return $this->success($this->_service->getTemplateListByAppProjectId($appProjectTypeId));
    }

    /**
     * 修改代码模板的服务器域名及业务域名
     *
     * @param $id
     * @param MiniProgramTemplateUpdateDomainRequest $request
     * @return mixed
     * @throws \Exception
     */
    public function updateDomain($id, MiniProgramTemplateUpdateDomainRequest $request) {
        return $this->success($this->_service->updateDomain($id, $request));
    }
    
    /**
     * 根据指定模板批量更新小程序
     *
     * @param $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function batchPublishMiniProgram($id) {
        //检测是否频繁操作，主要是根据此模板下面的项目数，进行下次可以操作的延迟操作时间限制
        $this->_service->batchPublishMiniProgramByTemplateIdNextOperationBufferCheck($id, MiniProgramTemplateService::BATCH_PUBLISH_MINI_PROGRAM_NEXT_OPERATION_BUFFER_CACHE_NAME_PRE);
        
        //暂时这样处理，以后可以做成异步任务，让其可并行执行处理
        ini_set('max_execution_time', '0');
        \App\Http\Middleware\TerminateTask::addTaskFnList(function () use ($id) {
            $this->_service->batchPublishMiniProgramByTemplateId($id);
        });
        
        return $this->success(null);
    }

    /**
     * 检查模板可用性
     *
     * @return mixed
     */
    public function check() {
        return $this->success($this->_service->check());
    }

    /**
     * 获取搜索过滤魔板列表
     * @return mixed
     */
    public function getTemplateListForSearchFilter() {
        return $this->success($this->_service->getTemplateListForSearchFilter());
    }

    /**
     * 修改审核项列表
     *
     * @param $id
     * @param MiniProgramTemplateUpdateAuditItemListRequest $request
     * @return mixed
     * @throws \Exception
     */
    public function updateAuditItemList($id, MiniProgramTemplateUpdateAuditItemListRequest $request) {
        return $this->success($this->_service->updateAuditItemList($id, $request));
    }
    
    /**
     * 回退该模板下小程序项目版本
     *
     * @param $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function batchRevertMiniProgramCodeRelease($id) {
        $this->_service->batchPublishMiniProgramByTemplateIdNextOperationBufferCheck($id, MiniProgramTemplateService::BATCH_REVERT_MINI_PROGRAM_CODE_RELEASE_NEXT_OPERATION_BUFFER_CACHE_NAME_PRE);
        
        //暂时这样处理，以后可以做成异步任务，让其可并行执行处理
        ini_set('max_execution_time', '0');
        \App\Http\Middleware\TerminateTask::addTaskFnList(function () use ($id) {
            $this->_service->batchRevertMiniProgramCodeReleaseByTemplateId($id);
        });
        
        return $this->success(null);
    }
}